<script>

let sourceList = [
  {
    id: 1,
    level: 1,
    name: '中国',
    value: 120,
    textList: [
      {
        id: 1,
        text: '错误内容'
      }
    ],
    children: [
      {
        id: 2,
        level: 2,
        name: '北京',
        value: 20,
        textList: [
          {
            id: 1,
            text: '错误内容'
          }
        ],
        children: [
          {
            id: 3,
            level: 3,
            name: '东城区',
            value: 30,
            textList: [
              {
                id: 1,
                text: '错误内容'
              }
            ],
          },
          {
            id: 4,
            level: 3,
            name: '海淀区',
            value: 40,
            textList: [
              {
                id: 1,
                text: '错误内容'
              }
            ],
          }
        ]
      },
      {
        id: 5,
        level: 2,
        name: '上海',
        value: 50,
        textList: [
          {
            id: 1,
            text: '错误内容'
          }
        ],
        children: [
          {
            id: 6,
            level: 3,
            name: '浦东新区',
            value: 60,
            textList: [
              {
                id: 1,
                text: '错误内容'
              }
            ],
          },
          {
            id: 7,
            level: 3,
            name: '黄浦区',
            value: 70,
            textList: [
              {
                id: 1,
                text: '错误内容'
              }
            ],
          }
        ]
      }
    ]
  },
  {
    id: 8,
    level: 1,
    name: '日本',
    value: 60,
    children: [
      {
        id: 9,
        level: 2,
        name: '大版',
        value: 70,
        children: [
          {
            id: 10,
            level: 3,
            name: '大版市区',
            value: 80
          },
          {
            id: 11,
            level: 3,
            name: '大版郊区',
            value: 90
          }
        ]
      },
      {
        id: 12,
        level: 2,
        name: '东京',
        value: 100,
        children: [
          {
            id: 13,
            level: 3,
            name: '东京市区',
            value: 110
          },
          {
            id: 14,
            level: 3,
            name: '东京郊区',
            value: 120
          }
        ]
      }
    ]
  }
]
export default {
  name: "index",
  data() {
    return {
      chartList: [],
      currentItem: {}
    }
  },
  mounted() {
    this.chartList.push({
      id: 0,
      level: 0,
      name: '初始化数据',
      children: [
        ...sourceList
      ]
    })
  },
  methods: {
    getOption(item) {
      return {
        tooltip: {
          trigger: 'item'
        },
        legend: {
          top: '5%',
          left: 'center'
        },
        series: [
          {
            name: item.name,
            type: 'pie',
            radius: ['40%', '70%'],
            center: ['50%', '50%'],
            avoidLabelOverlap: false,
            itemStyle: {
              borderRadius: 10,
              borderColor: '#fff',
              borderWidth: 2
            },
            labelLine: {
              show: true
            },
            label: {
              show: true,
              position: 'outside',
              formatter: '{b}: {c} ({d}%)'
            },
            emphasis: {
              label: {
                show: true,
                fontSize: 14,
                fontWeight: 'bold'
              }
            },
            data: item.children
          }
        ]
      }
    },
    clickChart(item,index, e) {
      if (item.children) {
        console.log(item,e)
        // 从当前索引之后数据全部清空
        this.chartList.splice(index + 1)
        let findItem = e.data
        if (findItem.children) {
          // 推送当前的数据
          this.chartList.push(findItem)
          this.isTextShow = true
        }
        this.currentItem = findItem
      }  else {
        this.currentItem = {}
      }
    },
  }
}
</script>

<template>
  <div class="home">
    <el-row :gutter="12" style="height: 100%">
      <el-col :span="10" style="height: 100%">
          <el-row :gutter="24" style="height: 100%">
            <el-col :span="12" v-for="(item,index) in chartList" :key="index" style="margin-bottom: 12px">
              <el-card shadow="none">
                <h2 class="title">
                  {{ item.name }}
                </h2>
                <v-chart autoresize :option="getOption(item)" @click="e => clickChart(item,index,e)" style="width: 100%;height: 300px"></v-chart>
              </el-card>
            </el-col>
          </el-row>
      </el-col>
      <el-col :span="14" style="height: 100%">
        <el-card class="box-card" style="height: 100%">
          <div slot="header" class="clearfix">
            <span>{{ currentItem.name }}</span>
          </div>
          <div v-for="o in currentItem.textList || []" :key="o" class="text item">
            {{o.text }}
          </div>
        </el-card>
      </el-col>
    </el-row>
  </div>
</template>

<style scoped lang="scss">
.home {
  height: 100%;
  background-color: #ffffff;
  padding: 40px;
  overflow-y: auto;
}
</style>
